package com.luck.emp.cg.web;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.emp.cg.logic.ICgLogic;
import com.luck.emp.cg.qry.CgQry;
import com.luck.emp.entity.EmpTCgD;
import com.luck.emp.entity.EmpTCgM;
import com.luck.emp.entity.EmpTThD;
import com.luck.emp.entity.EmpTThM;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class CgAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		ICgLogic cgLogic;
		/**
		 * 查询对象
		 */
		CgQry qry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		EmpTCgM entity;
		List<EmpTCgD> cgds;
		String[] productCodes;//商品数组
		String [] priceReals;//实际采购价
		String[] qtys;//数量数组
		Long[] ids;
		/**
		 * 采购单列表
		 */
		public String list() throws Exception{
			UserBean userInfo = super.getUserInfo();
		
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (CgQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new CgQry();
			}
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			qry.setUserInfo(userInfo);
			page = cgLogic.findCgPage(qry, Page.class);
			return "list";
		}
		/**
		 * 新增
		 **/
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = new EmpTCgM();
			super.event="ADD";
			super.title="新增商品采购";
			return "edit";
		}
		/**
		 * 修改
		 **/
		public String toEdit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = cgLogic.get(EmpTCgM.class,entity.getCgCode());
			if(!entity.getStatus().equals("1")){
				super.msgStr="此单非新建状态，不可修改";
				return GLOBAL_ERROR;
			}
			cgds = cgLogic.findCgd(entity.getCgCode());
			super.event="EDIT";
			super.title="修改商品采购";
			return "edit";
		}
		/**
		 * 修改动作
		 **/
		public String edit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if("ADD".equals(super.event)){
				//新增操作
				entity.setCgCode(null);
				cgLogic.saveCg(userInfo, entity,productCodes, priceReals, qtys, ids);
				super.addAttribute("qry.pk", entity.getCgCode());
			}else if("EDIT".equals(super.event)){
				//修改操作
				EmpTCgM db = cgLogic.get(EmpTCgM.class,entity.getCgCode());
				super.copyProperties(db, entity);
				cgLogic.saveCg(userInfo, db,productCodes, priceReals, qtys,ids);
				super.addAttribute("qryHex", super.qryHex);
			}
			super.redirectUrl = "/emp/cg_list.action";
			return super.GLOBAL_SUCCESS;
		}
		/**
		 * 删除
		 **/
		public String delete() throws Exception{			
			UserBean userInfo = super.getUserInfo();
			entity = cgLogic.get(EmpTCgM.class,entity.getCgCode());
			String str = "";
			try {
				str = this.cgLogic.deleteCg(userInfo, entity);;
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				this.msgStr = str;
				super.renderJsonError(str);
			}else
				super.renderJsonSuccess("删除成功!");
			return null;
		}
		
		/**
		 * 修改
		 **/
		public String toView() throws Exception{
			entity = cgLogic.get(EmpTCgM.class,entity.getCgCode());
			cgds = cgLogic.findCgd(entity.getCgCode());
			super.title="商品采购详细信息";
			return "view";
		}

		public String print() throws Exception{
			entity = cgLogic.get(EmpTCgM.class,entity.getCgCode());
			cgds = cgLogic.findCgd(entity.getCgCode());
			super.title="商品采购详细信息";
			return "print";
		}
		
		public CgQry getQry(){
			return this.qry;
		}
		public void setQry(CgQry qry){
			this.qry = qry;
		}
		
		public ICgLogic getCgLogic(){
			return this.cgLogic;
		}
		public void setCgLogic(ICgLogic cgLogic){
			this.cgLogic = cgLogic;
		}
		public Page getPage() {
			return this.page;
		}
		public void setPage(Page page) {
			this.page = page;
		}
		public EmpTCgM getEntity(){
			return entity;
		}
		public void setEntity(EmpTCgM entity){
			this.entity = entity;
		}
		public List<EmpTCgD> getCgds() {
			return cgds;
		}
		public void setCgds(List<EmpTCgD> cgds) {
			this.cgds = cgds;
		}
		public String[] getProductCodes() {
			return productCodes;
		}
		public void setProductCodes(String[] productCodes) {
			this.productCodes = productCodes;
		}
		public String[] getPriceReals() {
			return priceReals;
		}
		public void setPriceReals(String[] priceReals) {
			this.priceReals = priceReals;
		}
		public String[] getQtys() {
			return qtys;
		}
		public void setQtys(String[] qtys) {
			this.qtys = qtys;
		}
		public Long[] getIds() {
			return ids;
		}
		public void setIds(Long[] ids) {
			this.ids = ids;
		}
		
}